<?php
/**
 * 权限管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\content\controllers;

use app\libs\Pager;
use app\modules\content\models\IeltsWriteTag;
use yii;
use app\libs\ApiControl;
use app\modules\content\models\IeltsWrite;
use app\libs\GetData;

class WriteController extends ApiControl
{
    public $enableCsrfValidation = false;

    /**
     * [actionIndex description]
     * @Author don
     * @Date   2022-03-07
     * @return [type]     [description]
     */
    public function actionIndex()
    {
        $year = Yii::$app->request->get('year');
        $month = Yii::$app->request->get('month');
        $part = Yii::$app->request->get('part');
        $page = Yii::$app->request->get('page',1);
        $where = ' where 1=1';
        if ($year != false) {
            $where .= " and year=$year";
        }
        if ($month != false) {
            $where .= " and month=$month";
        }
        if ($part != false) {
            $where .= " and part=$part";
        }
        $offset = 15 * ($page - 1);
        $count = Yii::$app->db->createCommand("select count(*) as count from {{%ielts_write}} $where")->queryOne();
        $url = '/content/write/index?page';
        $count = $count['count'];
        $page = new Pager("$url", $count, $page, 15);
        $str = $page->GetPager();
        $data = Yii::$app->db->createCommand("select * from {{%ielts_write}} $where order by id desc limit $offset,15")->queryAll();
        return $this->render('index', ['data' => $data,'str'=>$str]);
    }

    public function actionAdd()
    {
        if (!$_POST) {
            $id = Yii::$app->request->get('id', '');
            $tags = IeltsWriteTag::find()->asArray()->limit(20)->all();
            foreach ($tags as $k=>$v){
                $tags[$k]['select'] = 0;
            }
            if (empty($id)) {
                return $this->render('add',['tags'=>$tags]);
            } else {
                $data = Yii::$app->db->createCommand("select * from {{%ielts_write}} where id=" . $id)->queryOne();
                if($data['tags']){
                    $names = array_column($tags,'name');
                    $arr = explode(',',$data['tags']);
                    foreach ($arr as $k=>$v){
                        $hasTag = [];
                        if(!$v) continue;
                        $key = array_search($v,$names);
                        if($key!==false){
                            $tags[$key]['select'] = 1;
                        }else{
                            $hasTag['id'] = 0;
                            $hasTag['name'] = $v;
                            $hasTag['select'] = 1;
                            $tags[] = $hasTag;
                        }
                    }
                }
                return $this->render('add', ['data' => $data,'tags'=>$tags]);
            }

        } else {
            $IeltsWrite = new IeltsWrite();
            $getdata = new GetData();
            $must = array('question' => '写作题目','year'=>'年份','part'=>'part');
            $data = $getdata->PostData($must, 'ielts_write');
            if($data['updateTime']) $data['updateTime'] = strtotime($data['updateTime']);
            if (empty($data['id'])) {
                $re = Yii::$app->db->createCommand()->insert("{{%ielts_write}}", $data)->execute();
            } else {
                $re = $IeltsWrite->updateAll($data, 'id=:id', array(':id' => $data['id']));
            }
            if ($re) {
                $this->redirect('index');
            } else {
                echo '<script>alert("数据修改/添加失败，请重试");history.go(-1);</script>';
                die;
            }
        }

    }

    public function actionDel()
    {
        $id = Yii::$app->request->get('id', '');
        $re = IeltsWrite::deleteAll("id=:id", array(':id' => $id));
        if ($re) {
            echo true;
        }

    }
}